﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeadlockDetection
{
    public class Process
    {
        public int processId;
        public int[] maxResourcesNeeded;
        public int[] resourcesAllocated;
        public int[] resourcesRequested;

        private Random random = new Random();

        public Process(int procId, int[] needed, int[] allocated, int[] requested)
        {
            processId = procId;
            maxResourcesNeeded = new int[needed.Length];
            resourcesAllocated = new int[allocated.Length];
            resourcesRequested = new int[requested.Length];

            for (int i = 0; i < needed.Length; i++)
            {
                maxResourcesNeeded[i] = needed[i];
                resourcesAllocated[i] = allocated[i];
                resourcesRequested[i] = requested[i];
            }
        }

        public Process(int procId, Resources resources)
        {
            processId = procId;
            maxResourcesNeeded = new int[resources.Length];
            resourcesAllocated = new int[resources.Length];
            resourcesRequested = new int[resources.Length];

            for (int i = 0; i < resources.Length; i++)
            {
                maxResourcesNeeded[i] = random.Next(0, resources[i].numberOfInstances + 1);
                resourcesAllocated[i] = random.Next(0, Math.Min(maxResourcesNeeded[i] + 1, resources[i].availableInstances + 1));
                resourcesRequested[i] = random.Next(0, maxResourcesNeeded[i] - resourcesAllocated[i] + 1);
                resources[i].availableInstances -= resourcesAllocated[i];
            }
        }

        public Process(int procId, Resources resources, bool single)
        {
            processId = procId;
            maxResourcesNeeded = new int[resources.Length];
            resourcesAllocated = new int[resources.Length];
            resourcesRequested = new int[resources.Length];

            for (int i = 0; i < resources.Length; i++)
            {
                maxResourcesNeeded[i] = random.Next(0,2);

                resourcesAllocated[i] = random.Next(0, Math.Min(maxResourcesNeeded[i], resources[i].availableInstances) + 1);
                resourcesRequested[i] = random.Next(0, maxResourcesNeeded[i] - resourcesAllocated[i] + 1);
                resources[i].availableInstances -= resourcesAllocated[i];
            }
        }

        public override string ToString()
        { return ("P" + processId); }
    }
}
